package com.freightoa.foundation.presentation.activity


import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.freightoa.foundation.viewmodel.BaseState
import com.freightoa.foundation.viewmodel.BaseViewModel
import com.freightoa.foundation.viewmodel.UiIntent
import dagger.hilt.android.AndroidEntryPoint

//@AndroidEntryPoint
abstract class BaseComposeFragment<State : BaseState, Intent : UiIntent, VM : BaseViewModel<State, Intent>> :
    Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return ComposeView(requireContext()).apply {
            setContent {
                val viewModel: VM = ViewModelProvider(requireActivity())[getViewModelClass()]
                val state by viewModel.uiState.collectAsStateWithLifecycle(
                    initialValue = createInitialState(),
                    lifecycle = lifecycle)
                Content(state, viewModel::handleIntent)
            }
        }
    }

    // 返回类型修改为 Class<VM>
    abstract fun getViewModelClass(): Class<VM>

    // 抽象方法，用于创建初始状态，具体实现由子类完成
    abstract fun createInitialState(): State

    @Composable
    abstract fun Content(state: State, sendIntent: (Intent) -> Unit)
}